<?php
 require "../document/class_corpus.php";
 require "../document/class_document.php";
 require "../document/class_document_xml.php";

 require "../files/tool_files_dirs.php";
 require "../misc/class_tag.php";

 require "../tree/class_tree.php";
 require "../tree/class_tree_xhtml.php";
 require "../tree/class_structure.php";
 require "../tree/tool_tree.php";

 require "../array/tool_array.php";

 require "../maths/tool_maths.php";

 require "../string/tool_string.php";
 require "../string/class_segment.php";

 require "../pdo/class_sql_document.php";

 require "../pear_graph/tool_pear_graph.php";

 require "../misc/tool_color.php";

function fill_graph(&$graph,$data_x,$data_y){
  fill_graph_aux($graph,$data_x,$data_y);
  return $graph->plot4();
}

function fill_graph_aux(&$graph,$data_x,$data_y){
  $graph->__add_array_x($data_x);
  $graph->__add_array_y($data_y);
}

function rep2log_rep(&$rep){
  $rep_log=array();
  $cpt=0;
  foreach($rep as $val){
    ++$cpt;
    $lcpt="".log($cpt);
    if($val==0) $lval="-0.2";
    else $lval="".log($val);
    $rep_log[$lcpt]=$lval;
  }
  return $rep_log;
}

$exp=new sql_experience("align_ecr_selected");
$array_obj_sql=$exp->reader_experience();
$array_data=array();

$data_lg=$array_obj_sql['sql_document']->reader_all_lg();

$corpus_multidoc = array();

//$data_lg = array( array('lg'=>'es'),array('lg'=>'fr') );
$id_chunk = 1;


  $tool_graph=new Tool_pear_graph();
  $tool_graph->__set_width(500);
  $tool_graph->__set_height(510);
  $tool_graph->__add_title("Loi de Zipf");

foreach($data_lg as $lg){
  $to_zipf = array();
  $res = $array_obj_sql['sql_rstr']->reader_lg2array($lg[0]);
  foreach($res as $l){
    $to_zipf[$l[0]] += $l[2];
  }

  $zipf=tool_maths::zipf($to_zipf);

  fill_graph_aux($tool_graph,array_keys($zipf),array_values($zipf));
  $tool_graph->__add_plot_title($lg[0]);
}
  $graph=$tool_graph->plot_log_log01();
  ob_start();
    $graph->done();
    $graph_svg=ob_get_contents();
  ob_clean();
  Tool_files::file_write("./zipf_rstr_corpus.svg",$graph_svg);
die();



$array_factory = array();
foreach($data_lg as $lg){
  $array_factory[$lg['lg']] = new factory_mot();
  $r = $array_obj_sql['sql_rstr']->reader_all2relative_id_doc($lg["lg"]);
  $k = key($r);
  foreach($r[$k] as $tid=>$id){
    $corpus_multidoc[$id][$k] = $tid;
  }
}

$doc2bloc = array();
$id_bloc2text = array();

//$array_word = $array_obj_sql['sql_chunk']->reader_id_bloc2info_chunk(2267);
//print_r($array_word);
//die();

foreach($corpus_multidoc as $id_multidoc=>$multidoc){
  foreach($multidoc as $lg=>$id_doc){
  $r = $array_obj_sql['sql_bloc']->reader_id_document2array($id_doc);
    foreach($r as $info_bloc){
      $id_bloc = $info_bloc['id_bloc'];
      $id_bloc2text[$id_bloc] = $info_bloc['contenu_text'];
      $array_word = $array_obj_sql['sql_chunk']->reader_id_bloc2info_chunk($id_bloc);
      $bloc2chunk[$lg][$id_doc][$id_bloc] = $array_word;
    }
  }
}

$id_multidoc = 1;
$multidoc = $corpus_multidoc[$id_multidoc];
$lg_src = 'fr';
$lg_tgt = 'es';
$id_doc_src = $multidoc[$lg_src];
$id_doc_tgt = $multidoc[$lg_tgt];

$bc_src = $bloc2chunk[$lg_src][$id_doc_src];
$bc_tgt = $bloc2chunk[$lg_tgt][$id_doc_tgt];

function bc2ac($v,&$txt){
  $ac = array();
  foreach($v as $id_chunk => $array_mot){
    $am = array();
    foreach($array_mot as $i){
      $mot = new mot($txt, $i[1], $i[2], array("type"=>$i[4]));
      $am[] = $mot;
    }
    $chunk = new chunk($am);
    $ac[] = $chunk;
  }
  return $ac;
}

function chunk2rstr($ac,$id_bloc,&$array_obj_sql){
  $res = array();
  foreach($ac as $id=>$c_src){
    $o_src = $c_src->__get_offset();
    $l_src = $c_src->__get_len();
    $candidats = $array_obj_sql['sql_rstr']->reader_seg2seg_inside($id_bloc,$o_src,$l_src);
    $inside = $union = $outside = array();
    foreach($candidats as $cand){
      (is_inside($o_src,$l_src,$c['offset'],$c['len'])) ? $inside[]=$cand : $outside[]=$cand;
    }
    $res[$id] = array('outside' => $outside, 'inside' => $inside);
  }
  return $res;
}


$time_start = microtime(true);
$cpt = 0;

//$bc = before chunk
//$ac = array chunk

$list_lg_v = array();

foreach($data_lg as $lg_info){
    $lg = $lg_info[0];
    $nb_chunk = 0;
    $list_lg_v[$lg] = array();
    foreach($bloc2chunk[$lg] as $bc_src){
        while(list($k1,$v1) = each($bc_src)){
            foreach($v1 as $aw){
              foreach($aw as $info_w){
                $type = $info_w[4];
                $graphie = $info_w[3];
                if($type == 'v'){
                    $list_lg_v[$lg][$graphie]++;
                }
              }
            }
        }
    }
}

foreach($list_lg_v as $lg=>$av){
  echo "$lg ".array_sum($av)."\n";
//  arsort($av);
//  foreach($av as $m => $eff){
//    if($lg != "el") echo "$m : $eff\n";
//    else echo "\\textgreek{} : $eff %%$m\n";
//  }
}

die();

while(list($k1,$v1) = each($bc_src) and list($k2,$v2) = each($bc_tgt)){
  $ac_src = bc2ac($v1,$id_bloc2text[$k1]);
  $ac_tgt = bc2ac($v2,$id_bloc2text[$k2]);
  if($cpt == 1){
    print_r($ac_tgt);
    foreach($ac_tgt as $c){
      print_r($c);
      print $id_bloc2text[$k2];
      print "\n";
      print $c;
      print "\n";
    }
  }
//  $chunk = reset($ac_src);
//  $g = $chunk->__get_str();
  $rstr_src = chunk2rstr($ac_src,$k1,$array_obj_sql);
  $rstr_tgt = chunk2rstr($ac_tgt,$k2,$array_obj_sql);

/*
  foreach($rstr_src as $id_chunk=>$info){
      $chunk = $ac_src[$id_chunk];
      $g = "$chunk";
      $o = $chunk->__get_offset();
      $l = $chunk->__get_len();
      echo "inside\n";
      print_inside($g,$o,$l,$info['inside']);
      echo "outside\n";
      print_outside($g,$o,$l,$info['outside']);
      die();
  }
*/
  if($cpt == 1) die();
  ++$cpt;
}
$time_end = microtime(true);
$time=$time_end-$time_start;
echo "time=$time\n";
  die();

?>
